package com.openbravo.pos.base;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Properties;
import java.util.logging.Logger;

import cn.ekuma.util.PropHelper;

public abstract class AbstractAppConfig implements I_AppConfig {

    private static Logger logger = Logger.getLogger("com.openbravo.pos.base.AbstractAppConfig");
     
    protected static Properties m_propsconfig;
    private File configfile;
    
    
    public AbstractAppConfig(String[] args) {
        if (args.length == 0) {
            init(getDefaultConfig());
        } else {
            init(new File(args[0]));
        }
    }
    
    public AbstractAppConfig(File configfile) {
        init(configfile);
    }
    
    private void init(File configfile) {
        this.configfile = configfile;
        m_propsconfig = new Properties();

        logger.info("Reading configuration file: " + configfile.getAbsolutePath());
    }

    public abstract File getDefaultConfig() ;

    public  String getProperty(String sKey) {
        return m_propsconfig.getProperty(sKey);
    }

    public static String getAppProperty(String sKey){
        return m_propsconfig.getProperty(sKey);
    }
    
    public static void setAppProperty(String sKey,String sValue){
        if (sValue == null) {
            m_propsconfig.remove(sKey);
        } else {
            m_propsconfig.setProperty(sKey, sValue);
        }
    }

    public String getHost() {
        return getProperty("machine.hostname");
    } 
    
    public File getConfigFile() {
        return configfile;
    }
    
    public void setProperty(String sKey, String sValue) {
        if (sValue == null) {
            m_propsconfig.remove(sKey);
        } else {
            m_propsconfig.setProperty(sKey, sValue);
        }
    }
    
    protected String getLocalHostName() {
        try {
            return java.net.InetAddress.getLocalHost().getHostName();
        } catch (java.net.UnknownHostException eUH) {
            return "localhost";
        }
    }
   
    public boolean delete() {
        loadDefault();
        return configfile.delete();
    }
    
    public void load() {
        loadDefault();
        try {
            InputStream in = PropHelper.guessPropFile(AbstractAppConfig.class,configfile.getName());
            if (in != null) {
                m_propsconfig.load(in);
                in.close();
            }
        } catch (IOException e){
            loadDefault();
        }
    }
    
    public void save() throws IOException {
        
        OutputStream out = new FileOutputStream(configfile);
        if (out != null) {
            m_propsconfig.store(out, AppLocal.APP_NAME + ". Configuration file.");
            out.close();
        }
    }
    
    public abstract void loadDefault() ;

	@Override
	public void merge(Properties p) {
		if(p.isEmpty())
			return ;
		Enumeration<?> set=p.propertyNames();	
		while(set.hasMoreElements()){
			Object key=set.nextElement();
		     m_propsconfig.put(key, p.get(key));
		}
	}
}
